package com.zengdada.authority.utils.transformdata;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author 12614
 * @contact 1261438511@qq.com
 * @date 2019/3/7 13:54
 */
public abstract class TransformData<T, V> {

    /**
     * 转换的方法
     *
     * @param obj 需要转换的值
     * @return
     */
    public abstract V transform(T obj);

    /**
     * 转换判断条件
     *
     * @param o 需要转换的值
     * @return
     */
    public abstract boolean equalType(Object o);

    public Class<Object> getT() {
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            Type[] arguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
            if (arguments == null || arguments.length < 2) {
                throw new RuntimeException("没有正确定义泛型类型");
            }
            return (Class<Object>) arguments[0];
        }
        throw new RuntimeException("没有正确定义泛型类型");
    }

    public Class<Object> getV() {
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            Type[] arguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
            if (arguments == null || arguments.length < 2) {
                throw new RuntimeException("没有正确定义泛型类型");
            }
            return (Class<Object>) arguments[0];
        }
        throw new RuntimeException("没有正确定义泛型类型");
    }
}
